﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Net.Chirash.Twitter.TwitterClient;
using System.Text.RegularExpressions;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents
{
    /// <summary>
    /// UserControl1.xaml の相互作用ロジック
    /// </summary>
    public partial class UpdateForm : UserControl
    {
        #region Constructors

        public UpdateForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Types

        public delegate void RefreshMainWindowCallback();

        #endregion

        #region Public Properties

        /// <summary>
        /// このUpdateFormが対応するStatusBoxに割り当てられたStatusのIDです。
        /// </summary>
        public long ID { private get; set; }

        public static RefreshMainWindowCallback RefreshMainWindow { private get; set; }

        #endregion

        #region Public Events

        public event EventHandler UpdateCancellationPending;

        #endregion

        #region Private Events

        private void TweetTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CharacterCountLabel.Content = 140 - TweetTextBox.Text.Length;
        }

        private void TweetTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter &&
                ((int)Keyboard.Modifiers & Properties.Settings.Default.UpdateModifierKeys) ==
                    Properties.Settings.Default.UpdateModifierKeys
               ) // Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)
            {
                Update();
            }
            else if (e.Key == Key.Escape)
            {
                //ESCキーが押された場合、Updateのキャンセルを通知します
                if (UpdateCancellationPending != null)
                    UpdateCancellationPending(this, EventArgs.Empty);
            }
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            Update();
        }

        #endregion

        #region Private Methods

        private void Update()
        {
            if (TweetTextBox.Text.Length > 0)
            {
                UpdateButton.IsEnabled = false;
                try
                {
                    TwitterCommand command = new TwitterCommand(App.CurrentApp.Connection);
                    if (Regex.IsMatch(TweetTextBox.Text, @"D .+"))
                    {
                        string withoutD = TweetTextBox.Text.Substring(2);
                        int indexOfSpace = withoutD.IndexOf(' ');

                        string screenName = withoutD.Substring(0, indexOfSpace);
                        string message = withoutD.Substring(indexOfSpace + 1);

                        command.NewDirectMessage(screenName, message);
                    }
                    else
                    {
                        command.Update(TweetTextBox.Text, ID);
                    }
                }
                catch (Exception ex)
                {
                    //TODO:Show on TextBox
                    //App.CurrentApp.ApplicationEventLog.WriteEntry(ex.ToString());
                }
                TweetTextBox.Text = "";
                UpdateButton.IsEnabled = true;
            }
            RefreshMainWindow();
        }

        #endregion
    }
}
